package it.sarasini.dmp;

import java.io.File;
import java.util.ArrayList;

public class FileManager {
	
	private static final String[] supportedTypes = { "3gp", "mp3", "mp4", "aac", "flac", "mid", "ogg", "wav" };
	private ArrayList<File> selectedFilesAndDirs;
	private ArrayList<File> allFiles;
	
	
	public FileManager() {
		
		selectedFilesAndDirs = new ArrayList<File>();
		allFiles = new ArrayList<File>();
	}
	
	public boolean addFileOrDirectory(String str) {
		
		File f = new File(str);
		
		if(f == null)
			return false;
		
		if(allFiles.contains(f))
			return true;
		
		selectedFilesAndDirs.add(f);
		
		if(f.isDirectory())
			addDirectory(f);
		else
			addFile(f);
		
		return true;
	}
	
	private void addDirectory(File d) {
		
		File[] files = d.listFiles();
		
		if(files == null)
			return;
		
		for(int i=0; i<files.length; i++) {
			
			if(files[i] == null)
				return;
			
			if(files[i].isDirectory())
				addDirectory(files[i]);
			else
				addFile(files[i]);
		}
	}
	
	private void addFile(File f) {
		
		if(isSupported(f)) {
		
			if(!allFiles.contains(f))
				allFiles.add(f);
		}
	}
	
	private boolean isSupported(File f) {
		
		for(int i=0; i<supportedTypes.length; i++) {
			
			if(f.getPath().endsWith(supportedTypes[i]))
				return true;
		}
		
		return false;
	}
	
	public ArrayList<File> getAllFiles() {
		
		return allFiles;
	}

}
